package com.ysg.widget.tab;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.ysg.R;
import com.ysg.base.BaseViewPagerAdapter;
import com.ysg.utils.YDisplayUtil;
import com.ysg.utils.YRecyclerViewUtil;

import java.util.Arrays;
import java.util.List;

/**
 * tab
 */
public class YTabLayout extends LinearLayout {
    public interface OnClickListener {
        void onSelectClick(int tab);
    }
    private OnClickListener mListener;
    public void setOnClickListener(OnClickListener listener) {
        mListener = listener;
    }

    private RecyclerView recyclerView;
    private TabAdapter adapter;
    private ViewPager viewPager;

    public YTabLayout(Context context) {
        super(context);
    }

    public YTabLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        View view = View.inflate(context, R.layout.widget_tab_layout, this);
        recyclerView = view.findViewById(R.id.recyclerView);

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.YTabLayout);
        int lineDrawable = array.getResourceId(R.styleable.YTabLayout_lineDrawable, 0);
        int selectTextColor = array.getColor(R.styleable.YTabLayout_selectTextColor, 0);
        int unselectTextColor = array.getColor(R.styleable.YTabLayout_unselectTextColor, 0);
        int selectTextSize = array.getDimensionPixelSize(R.styleable.YTabLayout_selectTextSize, YDisplayUtil.dp2px(getContext(), 0));
        int unselectTextSize = array.getDimensionPixelSize(R.styleable.YTabLayout_unselectTextSize, YDisplayUtil.dp2px(getContext(), 0));
        boolean selectedBold = array.getBoolean(R.styleable.YTabLayout_selectedBold, false);
        boolean lineVisible = array.getBoolean(R.styleable.YTabLayout_lineVisible, true);
        array.recycle();

        adapter = new TabAdapter(lineDrawable, selectTextColor, selectTextSize, unselectTextColor, unselectTextSize, selectedBold, lineVisible);
    }

    public void initGrid(Fragment fragment, ViewPager viewPager, String[] items, List<Fragment> fragments){
        this.viewPager = viewPager;
        YRecyclerViewUtil.initGrid(getContext(), recyclerView, adapter, items.length);
        adapter.setList(Arrays.asList(items));

        viewPager.setAdapter(new BaseViewPagerAdapter(fragment.getChildFragmentManager(), fragments));
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
            @Override
            public void onPageScrollStateChanged(int state) {}
            @Override
            public void onPageSelected(int position) {
                adapter.setCurrentTab(position);
            }
        });
    }

    public void setCurrentTab(int tab){
        adapter.setCurrentTab(tab);
        if (viewPager != null){
            viewPager.setCurrentItem(tab);
        }
    }

    public void initGrid(String[] items){
        YRecyclerViewUtil.initGrid(getContext(), recyclerView, adapter, items.length);
        adapter.setList(Arrays.asList(items));
    }




    //=======================================TabAdapter===========================================
    public class TabAdapter extends BaseQuickAdapter<String, BaseViewHolder> {
        private int tab;
        private int lineDrawable;
        private int selectTextColor, unselectTextColor;
        private int selectTextSize, unselectTextSize;
        private boolean selectedBold, lineVisible;
        public TabAdapter(int lineDrawable, int selectTextColor, int selectTextSize, int unselectTextColor, int unselectTextSize, boolean selectedBold, boolean lineVisible) {
            super(R.layout.item_tab_layout);
            this.lineDrawable = lineDrawable;
            this.selectTextColor = selectTextColor;
            this.selectTextSize = selectTextSize;
            this.unselectTextColor = unselectTextColor;
            this.unselectTextSize = unselectTextSize;
            this.selectedBold = selectedBold;
            this.lineVisible = lineVisible;
        }

        public void setCurrentTab(int tab){
            this.tab = tab;
            notifyDataSetChanged();
        }

        @Override
        protected void convert(@NonNull BaseViewHolder holder, String item) {
            TextView tvTitle = holder.getView(R.id.tvTitle);
            ImageView ivLine = holder.getView(R.id.ivLine);
            View parent = holder.getView(R.id.parent);

            tvTitle.setText(item);

            if (tab == holder.getAbsoluteAdapterPosition()) {
                ivLine.setVisibility(lineVisible? VISIBLE: GONE);
                if (selectTextSize != 0){
                    tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, selectTextSize);
                }else{
                    tvTitle.setTextSize(15);
                }
                if (selectTextColor != 0){
                    tvTitle.setTextColor(selectTextColor);
                }else{
                    tvTitle.setTextColor(getContext().getResources().getColor(R.color.pinkColor));
                }
                if (selectedBold){
                    tvTitle.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                }
            } else {
                ivLine.setVisibility(lineVisible? View.INVISIBLE: GONE);
                if (unselectTextSize != 0){
                    tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, unselectTextSize);
                }else{
                    tvTitle.setTextSize(12);
                }
                if (unselectTextColor != 0){
                    tvTitle.setTextColor(unselectTextColor);
                }else{
                    tvTitle.setTextColor(getContext().getResources().getColor(R.color.grayColor));
                }
                tvTitle.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            }

            if (lineDrawable != 0){
                ivLine.setBackgroundResource(lineDrawable);
            }



            parent.setOnClickListener((view -> {
                tab = holder.getBindingAdapterPosition();
                notifyDataSetChanged();
                if (viewPager != null){
                    viewPager.setCurrentItem(tab);
                }
                if (mListener != null){
                    mListener.onSelectClick(tab);
                }
            }));
        }

    }


}
